﻿Imports CapaLogica.CL.Carrera
Imports CapaLogica.CL.PeriodoLectivo


Public Class frmBuscarCarrera


    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        If ((dataGridCarrera.CurrentRow Is Nothing) OrElse (dataGridCarrera.SelectedRows.Count = 0)) Then
            MsgBox("Debe de seleccionar un curso antes de consultar", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        Else
            Dim id As Int32 = CStr(DataGridCarrera.SelectedRows(0).Cells(0).Value)
            Dim codigo As String = CStr(DataGridCarrera.SelectedRows(0).Cells(1).Value)
            Dim nombre As String = CStr(DataGridCarrera.SelectedRows(0).Cells(2).Value)
            Dim descripcion As String = CStr(DataGridCarrera.SelectedRows(0).Cells(3).Value)
            Dim idDirector As Int32 = CStr(DataGridCarrera.SelectedRows(0).Cells(5).Value)

            Dim frmConsultar As New frmConsultarCarrera(id, codigo, nombre, descripcion, idDirector)
            frmConsultar.ShowDialog()
            refrescar()
        End If
    End Sub

    Private Sub tsbRefrescar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbRefrescar.Click
        refrescar()
    End Sub

    Private Sub refrescar()

        txtCodigo.Text = Nothing
        txtNombre.Text = Nothing
        DataGridCarrera.DataSource = Nothing
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        Dim nombre As String
        Dim codigo As String
        nombre = Nothing
        codigo = Nothing

        If txtNombre.Text = Nothing And txtCodigo.Text = Nothing Then
            nombre = Nothing
            codigo = Nothing
        ElseIf txtNombre.Text = Nothing And txtCodigo.Text <> Nothing Then
            nombre = Nothing
            codigo = txtCodigo.Text
        ElseIf txtNombre.Text <> Nothing And txtCodigo.Text = Nothing Then
            codigo = Nothing
            nombre = txtNombre.Text

        ElseIf txtNombre.Text <> Nothing And txtCodigo.Text <> Nothing Then
            codigo = txtCodigo.Text
            nombre = txtNombre.Text

        End If

        dataGridCarrera.DataSource = GestorCarrera.buscarCarrera(codigo, nombre)

    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        Try


            If ((DataGridCarrera.CurrentRow Is Nothing) OrElse (DataGridCarrera.SelectedRows.Count = 0)) Then
                MsgBox("Debe de seleccionar una carrera antes de eliminarla", MsgBoxStyle.Exclamation, "Error")
                Exit Sub
            Else
                GestorPeriodoLectivo.validarQuePeriodoLectivoEstaCerrado("eliminar carrera")
                Dim mensaje As String
                mensaje = "¿Desea eliminar la carrera?"
                Dim button As DialogResult = MessageBox.Show(mensaje, "Usuario", MessageBoxButtons.YesNoCancel,
                                                            MessageBoxIcon.Warning)
                If button = DialogResult.Yes Then
                    Try
                        GestorCarrera.eliminarCarrera(Convert.ToString(DataGridCarrera.CurrentRow.Cells(0).Value.ToString()))
                        MsgBox("La carrera se ha eliminado con éxito")

                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
                    End Try
                Else
                    MsgBox("La carrera no se ha eliminado")
                End If

            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
        End Try

       

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Dispose()

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Me.Dispose()

    End Sub
End Class